package com.example.databindingtest.base

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

abstract class BaseVmFragment<T : ViewDataBinding, VM : ViewModel> : BaseViewFragment<T>() {

    protected lateinit var viewModel: VM

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        //创建ViewModel
        initViewModel()
        //观察ViewModel的数据变化--->更新ui
        observerData()
        //初始化相关控件
        initView()
        //设置相关的事件
        initEvent()
        //开始去加载数据
        startLoadData()
    }


    private fun initViewModel() {
        viewModel =
            ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(getSubVmClass())
    }

    abstract fun getSubVmClass(): Class<VM>

    open fun observerData() {

    }

    open fun initView() {

    }

    open fun initEvent() {

    }

    open fun startLoadData() {

    }
}